package com.hfut.jpoa.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.dao.CustomerDAO;
import com.hfut.jpoa.dto.CustomerDTO;
import com.hfut.jpoa.model.Customer;
import com.hfut.jpoa.service.CustomerService;
import com.hfut.jpoa.service.exception.CustomerException;

public class CustomerServiceImpl implements CustomerService
{
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(CustomerServiceImpl.class);
	private CustomerDAO customerDAO;

	public List<Customer> queryCustomers(Customer customer)
	        throws CustomerException
	{
		if (null == customer)
		{
			LOGGER.error("queryCustomers异常：值为空 ");
			throw new CustomerException("queryCustomers异常：值为空 ");
		}
		return customerDAO.queryCustomers(customer);
	}

	public CustomerDAO getCustomerDAO()
	{
		return customerDAO;
	}

	public void setCustomerDAO(CustomerDAO customerDAO)
	{
		this.customerDAO = customerDAO;
	}

	public ResultDto<Customer> queryInfoForPage(CustomerDTO dto)
	        throws CustomerException
	{
		if (null == dto)
		{
			LOGGER.error("queryInfoForPage异常：查询条件为空 ");
			throw new CustomerException("queryInfoForPage异常：查询条件为空 ");
		}
		return customerDAO.findInfoForPage(dto);

	}

	public void saveCustomer(Customer condition) throws CustomerException
	{
		if (null == condition)
		{
			LOGGER.error("saveCustomer异常：值为空 ");
			throw new CustomerException("saveCustomer异常：值为空 ");
		}
		customerDAO.insert(condition);
	}

	public void updateCustomer(Customer customer) throws CustomerException
	{
		if (null == customer)
		{
			LOGGER.error("updateCustomer异常：值为空 ");
			throw new CustomerException("updateCustomer异常：值为空 ");
		}
		customerDAO.updateByPrimaryKeySelective(customer);
	}

	public Customer findCustomer(Customer customer) throws CustomerException
	{
		if (null == customer)
		{
			LOGGER.error("updateCustomer异常：值为空 ");
			throw new CustomerException("updateCustomer异常：值为空 ");
		}
		Integer id = customer.getId();
		return customerDAO.selectByPrimaryKey(id);
	}

}
